<template>
  <div>
    <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="80px"
      >
        <el-form-item label="模版名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="请输入模版名称"></el-input>
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="dataForm.version" placeholder="请输入版本号（数字）" type="number" maxlength="3"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="dataForm.description" placeholder="请填写描述"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      roleList: [],
      url: "",
      urlVisible: false,
      dataForm: {
        id: 0,
        name: "",
        version: "",
        description: ""
      },
      dataRule: {
        name: [
          { required: true, message: "模版名称不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/activiti/process/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              version: this.dataForm.version,
              description: this.dataForm.description
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.visible = false;
              this.urlVisible = true;
              this.url =
                process.env.NODE_ENV !== "production" && process.env.OPEN_PROXY
                  ? "/proxyApi/"
                  : window.SITE_CONFIG.baseUrl +
                    "/modeler.html?modelId=" +
                    data.id;
              this.$router.push({ path: "/modeler", query: { url: this.url } });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
